package com.brspace.qualitycontrol.service.impl;

import com.brspace.qualitycontrol.entity.CollectWrit;
import com.brspace.qualitycontrol.entity.NewNotice;
import com.brspace.qualitycontrol.entity.vo.NoticeTypeVo;
import com.brspace.qualitycontrol.entity.vo.NoticeVo;
import com.brspace.qualitycontrol.entity.vo.PaginationVo;
import com.brspace.qualitycontrol.mapper.NewNoticeMapper;
import com.brspace.qualitycontrol.service.NewNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brspace.qualitycontrol.utils.EmptyUtil;
import com.brspace.qualitycontrol.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 新建通知 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-02
 */
@Service
public class NewNoticeServiceImpl extends ServiceImpl<NewNoticeMapper, NewNotice> implements NewNoticeService {
    @Resource
    private NewNoticeMapper newNoticeMapper;

    /**
     * 新建新闻通知
     * @param newNotice
     * @return
     */
    @Override
    public R addNew(NewNotice newNotice) {
        if (EmptyUtil.isEmpty(newNotice.getTitle())) {
            throw new SecurityException("标题不能为空！");
        }
        if (EmptyUtil.isEmpty(newNotice.getNoticePeople())){
            throw new SecurityException("通知人员不能为空");
        }
        if (newNoticeMapper.insert(newNotice)>0) {
            return R.ok().message("成功！");
        }
        return R.error().message("失败！");
    }

    //查询指定日期的通知消息
    @Override
    public List<NoticeVo> getDayList(Date noticeDate) {
        return newNoticeMapper.getDayList(noticeDate);
    }

    //获取今天之后有通知的日期
    @Override
    public List<String> getNoticeAfter() {
        return newNoticeMapper.getNoticeAfter();
    }

    //分页查询会议和通知消息
    @Override
    public PaginationVo<NoticeTypeVo> selectAllList(Integer pageNum, Integer pageSize) {
        PaginationVo<NoticeTypeVo> vo = new PaginationVo<>();
        //pageNum:查询的页数，pageSize:一页显示的数量
        PageHelper.startPage(pageNum,pageSize);
        List<NoticeTypeVo> consumerList = newNoticeMapper.selectAllList();
        //获取总记录数pageInfo.getTotal()
        PageInfo<NoticeTypeVo> pageInfo = new PageInfo<>(consumerList);
        vo.setTotal(Integer.parseInt(String.valueOf(pageInfo.getTotal())));
        vo.setDataList(consumerList);
        return vo;
    }
}
